<?php

class ZFKiller_Db_Table extends Zend_Db_Table_Abstract {

	protected $_errors = array();
	protected $_messageTemplates = array();

	public function addError($errorNumber) {
		$this->_errors[] = $errorNumber;
	}

	public function clearErrors() {
		$this->_errors = array();
	}
	
	public function getDateBd($time=null) {
		return date('c', (is_null($time) ? time() : $time));
	}
	
	/**
	 * Semelhante a find()
	 *
	 * @param string $key
	 * @return Zend_Db_Table_Row_Abstract|null
	 */
	public function findRow($key) {
		return $this->find($key)->current();
	}

	public function getErrors() {
		return $this->_errors;
	}

	public function getErrorsCount() {
		return count($this->_errors);
	}

	public function getErrorsMessages($data=array()) {
		if (count($this->_errors) == 0) {
			return array();
		}

		$dataReplace = array();
		foreach ($data as $dataKey => $dataValue) {
			$dataReplace['%' . $dataKey . '%'] = $dataValue;
		}

		$r = array();
		foreach ($this->_errors as $errorCode) {
			$r[$errorCode] = strtr($this->_messageTemplates[$errorCode], $dataReplace);
		}

		return $r;
	}

	public function getTableName() {
		return $this->_name;
	}

}